/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gicom.config;

import gicom.GenericServer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 *
 * @author guillaume
 */
public abstract class GenericConfig {
    
    private String prefix;
    private String id;
    
    protected abstract void doSave () throws Exception;
    protected abstract void doLoad () throws Exception;
    
    public void save () throws Exception {
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    GenericConfig.this.doSave();
                } catch (Exception ex) {
                    this.interrupt();
                }
            }
        };
        t.start();
        synchronized (GenericServer.defaultGenericServer.getGenericManager()) {
            while (t.isAlive()) GenericServer.defaultGenericServer.getGenericManager().wait(100);
        }
        if (t.isInterrupted()) throw new Exception("Save failed");
    }
    
    public void load () throws Exception {
        Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    GenericConfig.this.doLoad();
                } catch (Exception ex) {
                    this.interrupt();
                }
            }
        };
        t.start();
        synchronized (GenericServer.defaultGenericServer.getGenericManager()) {
            while (t.isAlive()) GenericServer.defaultGenericServer.getGenericManager().wait(100);
        }
        if (t.isInterrupted()) throw new Exception("Load failed");
    }
    
    public void destroy () {
        new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId()).delete();
        new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId() + "_tmp").delete();
    }
    
    public GenericConfig (String prefix, String id) throws Exception {
        this.prefix = prefix;
        this.id = id;
    }
    
    protected void saveProperties (Properties properties) throws Exception {
        File file = new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId());
        File temp = new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId() + "_tmp");
        temp.delete();
        temp.createNewFile();
        FileOutputStream fos = new FileOutputStream(temp);
        properties.store(fos, "");
        fos.close();
        file.delete();
        if (!temp.renameTo(file)) throw new Exception("Error while renaming the temp file.");
    }
    
    protected Properties loadProperties () throws Exception {
        File file = new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId());
        File temp = new File(GenericServer.defaultGenericServer.getDirectory(), getPrefix() + getId() + "_tmp");
        if (!file.exists() && !temp.exists()) throw new Exception("The configuration file " + getPrefix() + getId() + " does not exists.");
        if (!file.exists()) {
            if (!temp.renameTo(file)) throw new Exception("Error while renaming the temp file.");
            temp.delete();
            file = temp;
        }
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        fis.close();
        return properties;
    }
    
    public String getPrefix() {
        return prefix;
    }
    
    public String getId() {
        return id;
    }
}
